2020-2021 Sunseeker Telemetry and Lighting System
adc12_b_ex2_sharedRef.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // MSP430FR59xx Demo - ADC12B, Sample A1, 1.2V Shared Ref, Set P1.0 if A1 > 0.5V
34 //
35 // Description: A single sample is made on A1 with reference to internal
36 // 1.2V Vref. Software sets ADC12BSC to start sample and conversion - ADC10SC
37 // automatically cleared at EOC. ADC12B internal oscillator times sample (16x)
38 // and conversion. In Mainloop MSP430 waits in LPM0 to save power until ADC10
39 // conversion complete, ADC12_B_ISR will force exit from LPM0 in Mainloop on
40 // reti. If A1 > 0.5V, P1.0 set, else reset.
41 //
42 // MSP430FR5969
43 // -----------------
44 // /|\| XIN|-
45 // | | |
46 // --|RST XOUT|-
47 // | |
48 // >---|P1.1/A1 P1.0|-->LED
49 //
50 //******************************************************************************
51 #include "driverlib.h"
52 
53 void main(void)
54 {
55  // Stop WDT
56  WDT_A_hold(WDT_A_BASE);
57  //Set P1.0 as an output pin.
58  /*
59 
60  * Select Port 1
61  * Set Pin 0 as output
62  */
63  GPIO_setAsOutputPin(
64  GPIO_PORT_P1,
65  GPIO_PIN0
66  );
67 
68  //Set P1.0 as Output Low.
69  /*
70 
71  * Select Port 1
72  * Set Pin 0 to output Low.
73  */
75  GPIO_PORT_P1,
76  GPIO_PIN0
77  );
78  //Set P1.1 as Ternary Module Function Output.
79  /*
80 
81  * Select Port 1
82  * Set Pin 1 to output Ternary Module Function, (A1, C1, VREF+, VeREF+).
83  */
84  GPIO_setAsPeripheralModuleFunctionOutputPin(
85  GPIO_PORT_P1,
86  GPIO_PIN1,
87  GPIO_TERNARY_MODULE_FUNCTION
88  );
89 
90  /*
91  * Disable the GPIO power-on default high-impedance mode to activate
92  * previously configured port settings
93  */
94  PMM_unlockLPM5();
95 
96  //If ref generator busy, WAIT
97  while (Ref_A_isRefGenBusy(REF_A_BASE)) ;
98 
99  //Select internal ref = 1.2V
100  Ref_A_setReferenceVoltage(REF_A_BASE,
101  REF_A_VREF1_2V);
102 
103  //Turn on Reference Voltage
104  Ref_A_enableReferenceVoltage(REF_A_BASE);
105 
106  //Initialize the ADC12B Module
107  /*
108  * Base address of ADC12B Module
109  * Use internal ADC12B bit as sample/hold signal to start conversion
110  * USE MODOSC 5MHZ Digital Oscillator as clock source
111  * Use default clock divider/pre-divider of 1
112  * Not use internal channel
113  */
114  ADC12_B_initParam initParam = {0};
115  initParam.sampleHoldSignalSourceSelect = ADC12_B_SAMPLEHOLDSOURCE_SC;
116  initParam.clockSourceSelect = ADC12_B_CLOCKSOURCE_ADC12OSC;
117  initParam.clockSourceDivider = ADC12_B_CLOCKDIVIDER_1;
118  initParam.clockSourcePredivider = ADC12_B_CLOCKPREDIVIDER__1;
119  initParam.internalChannelMap = ADC12_B_NOINTCH;
120  ADC12_B_init(ADC12_B_BASE, &initParam);
121 
122  //Enable the ADC12B module
123  ADC12_B_enable(ADC12_B_BASE);
124 
125  /*
126  * Base address of ADC12B Module
127  * For memory buffers 0-7 sample/hold for 64 clock cycles
128  * For memory buffers 8-15 sample/hold for 4 clock cycles (default)
129  * Disable Multiple Sampling
130  */
131  ADC12_B_setupSamplingTimer(ADC12_B_BASE,
132  ADC12_B_CYCLEHOLD_16_CYCLES,
133  ADC12_B_CYCLEHOLD_4_CYCLES,
134  ADC12_B_MULTIPLESAMPLESDISABLE);
135 
136  //Configure Memory Buffer
137  /*
138  * Base address of the ADC12B Module
139  * Configure memory buffer 0
140  * Map input A1 to memory buffer 0
141  * Vref+ = IntBuffer
142  * Vref- = AVss
143  * Memory buffer 0 is not the end of a sequence
144  */
145  ADC12_B_configureMemoryParam configureMemoryParam = {0};
146  configureMemoryParam.memoryBufferControlIndex = ADC12_B_MEMORY_0;
147  configureMemoryParam.inputSourceSelect = ADC12_B_INPUT_A1;
148  configureMemoryParam.refVoltageSourceSelect = ADC12_B_VREFPOS_INTBUF_VREFNEG_VSS;
149  configureMemoryParam.endOfSequence = ADC12_B_NOTENDOFSEQUENCE;
150  configureMemoryParam.windowComparatorSelect = ADC12_B_WINDOW_COMPARATOR_DISABLE;
151  configureMemoryParam.differentialModeSelect = ADC12_B_DIFFERENTIAL_MODE_DISABLE;
152  ADC12_B_configureMemory(ADC12_B_BASE, &configureMemoryParam);
153 
154  ADC12_B_clearInterrupt(ADC12_B_BASE,
155  0,
156  ADC12_B_IFG0
157  );
158 
159  //Enable memory buffer 0 interrupt
160  ADC12_B_enableInterrupt(ADC12_B_BASE,
161  ADC12_B_IE0,
162  0,
163  0);
164 
165  __delay_cycles(75); // reference settling ~75us
166 
167  while(1)
168  {
169  __delay_cycles(5000); // Delay between conversions
170 
171  //Enable/Start sampling and conversion
172  /*
173  * Base address of ADC12B Module
174  * Start the conversion into memory buffer 0
175  * Use the single-channel, single-conversion mode
176  */
177  ADC12_B_startConversion(ADC12_B_BASE,
178  ADC12_B_MEMORY_0,
179  ADC12_B_SINGLECHANNEL);
180 
181  __bis_SR_register(LPM0_bits + GIE); // LPM0, ADC10_ISR will force exit
182  __no_operation(); // For debug only
183  }
184 }
185 
186 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
187 #pragma vector=ADC12_VECTOR
188 __interrupt
189 #elif defined(__GNUC__)
190 __attribute__((interrupt(ADC12_VECTOR)))
191 #endif
192 void ADC12_ISR(void)
193 {
194  switch(__even_in_range(ADC12IV,12))
195  {
196  case 0: break; // Vector 0: No interrupt
197  case 2: break; // Vector 2: ADC12BMEMx Overflow
198  case 4: break; // Vector 4: Conversion time overflow
199  case 6: break; // Vector 6: ADC12BHI
200  case 8: break; // Vector 8: ADC12BLO
201  case 10: break; // Vector 10: ADC12BIN
202  case 12: // Vector 12: ADC12BMEM0 Interrupt
203  if (ADC12_B_getResults(ADC12_B_BASE, ADC12_B_MEMORY_0) >= 0x6B4)
204  {
205  //Set P1.0 LED on
206  /*
207 
208  * Select Port 1
209  * Set Pin 0 to output high.
210  */
212  GPIO_PORT_P1,
213  GPIO_PIN0
214  );
215  }
216  else
217  {
218  //Set P1.0 LED off
219  /*
220 
221  * Select Port 1
222  * Set Pin 0 to output high.
223  */
225  GPIO_PORT_P1,
226  GPIO_PIN0
227  );
228  }
229  __bic_SR_register_on_exit(LPM0_bits); // Exit active CPU
230  break; // Clear CPUOFF bit from 0(SR)
231  case 14: break; // Vector 14: ADC12BMEM1
232  case 16: break; // Vector 16: ADC12BMEM2
233  case 18: break; // Vector 18: ADC12BMEM3
234  case 20: break; // Vector 20: ADC12BMEM4
235  case 22: break; // Vector 22: ADC12BMEM5
236  case 24: break; // Vector 24: ADC12BMEM6
237  case 26: break; // Vector 26: ADC12BMEM7
238  case 28: break; // Vector 28: ADC12BMEM8
239  case 30: break; // Vector 30: ADC12BMEM9
240  case 32: break; // Vector 32: ADC12BMEM10
241  case 34: break; // Vector 34: ADC12BMEM11
242  case 36: break; // Vector 36: ADC12BMEM12
243  case 38: break; // Vector 38: ADC12BMEM13
244  case 40: break; // Vector 40: ADC12BMEM14
245  case 42: break; // Vector 42: ADC12BMEM15
246  case 44: break; // Vector 44: ADC12BMEM16
247  case 46: break; // Vector 46: ADC12BMEM17
248  case 48: break; // Vector 48: ADC12BMEM18
249  case 50: break; // Vector 50: ADC12BMEM19
250  case 52: break; // Vector 52: ADC12BMEM20
251  case 54: break; // Vector 54: ADC12BMEM21
252  case 56: break; // Vector 56: ADC12BMEM22
253  case 58: break; // Vector 58: ADC12BMEM23
254  case 60: break; // Vector 60: ADC12BMEM24
255  case 62: break; // Vector 62: ADC12BMEM25
256  case 64: break; // Vector 64: ADC12BMEM26
257  case 66: break; // Vector 66: ADC12BMEM27
258  case 68: break; // Vector 68: ADC12BMEM28
259  case 70: break; // Vector 70: ADC12BMEM29
260  case 72: break; // Vector 72: ADC12BMEM30
261  case 74: break; // Vector 74: ADC12BMEM31
262  case 76: break; // Vector 76: ADC12BRDY
263  default: break;
264  }
265 }
void main(void)
void ADC12_ISR(void)
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)